package com.ty.utils;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;

import java.lang.reflect.Field;
import java.util.Set;

/**
 * @Author Tommy
 * @Date 2023/10/25
 */
public class QuartzUtil {

    public static void printInfo(Scheduler scheduler) {
        try {
            Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.anyJobGroup());
            System.out.println("Job Key集合：");
            System.out.println(jobKeys);
            jobKeys.stream().forEach(k -> {
                System.out.println("Group: " + k.getGroup());
                System.out.println("Name: " + k.getName());
            });
            System.out.println();

            Set<TriggerKey> triggerKeys = scheduler.getTriggerKeys(GroupMatcher.anyTriggerGroup());
            System.out.println("Trigger Key集合：");
            System.out.println(triggerKeys);
            triggerKeys.stream().forEach(k -> {
                System.out.println("Group: " + k.getGroup());
                System.out.println("Name: " + k.getName());
            });
            System.out.println();

            if (jobKeys.size() > 0) {
                JobDetail myJobDetail = scheduler.getJobDetail(jobKeys.stream().findFirst().get());
                System.out.println("查询JobDetail");
                System.out.println("Class: " + myJobDetail.getJobClass());
                System.out.println("描述：" + myJobDetail.getDescription());
                System.out.println("Durable：" + myJobDetail.isDurable());
                System.out.println("禁止并发执行：" + myJobDetail.isConcurrentExectionDisallowed());
                System.out.println("允许持久化：" + myJobDetail.isPersistJobDataAfterExecution());
                JobDataMap jobDataMap = myJobDetail.getJobDataMap();
                System.out.println("Job DataMap Size：" + jobDataMap.size());
                System.out.print("Job DataMap KeySet：");
                jobDataMap.keySet().stream().forEach(k -> System.out.print(k + "\t"));
                System.out.println();
                System.out.println();
            }

            if (triggerKeys.size() > 0) {
                Trigger myJobTrigger = scheduler.getTrigger(triggerKeys.stream().findFirst().get());
                System.out.println("查询Trigger");
                System.out.println("描述：" + myJobTrigger.getDescription());

                JobDataMap jobDataMap = myJobTrigger.getJobDataMap();
                System.out.println("Job DataMap Size：" + jobDataMap.size());
                System.out.print("Job DataMap KeySet：");
                jobDataMap.keySet().stream().forEach(k -> System.out.print(k + "\t"));
                System.out.println();

                System.out.println("优先级: " + myJobTrigger.getPriority());
                System.out.println("CalendarName: " + myJobTrigger.getCalendarName());
                System.out.println("StartTime: " + myJobTrigger.getStartTime());
                System.out.println("EndTime: " + myJobTrigger.getEndTime());
                System.out.println("FinalFireTime: " + myJobTrigger.getFinalFireTime());
                System.out.println("PreviousFireTime: " + myJobTrigger.getPreviousFireTime());
                System.out.println("NextFireTime: " + myJobTrigger.getNextFireTime());
                System.out.println("Misfire机制: " + myJobTrigger.getMisfireInstruction());
                System.out.println("ScheduleBuilder: " + myJobTrigger.getScheduleBuilder());

                ScheduleBuilder scheduleBuilder = myJobTrigger.getScheduleBuilder();
                if (scheduleBuilder instanceof CronScheduleBuilder) {
                    Field cronField = FieldUtils.getField(CronScheduleBuilder.class, "cronExpression", true);
                    String cron = cronField.get(scheduleBuilder).toString();
                    System.out.println("Cron表达式：" + cron);
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
